import jsonpath
import requests

from base.base_api import BaseApi
from utils.log_util import logger
from utils.utils import Utils


class Bzy(BaseApi):
    """
    完成对八爪云接口 token 的获取
    """

    def __init__(self):
        self.get_config()
        self.token = self.get_bzy_token()

    # 获取token
    def get_bzy_token(self):
        base_url = self.base_url
        url = f"{base_url}/login"
        logger.info(f"请求的url为{url}")
        json = {
            "username": self.username,
            "password": self.password
        }

        r = {
            "method": "POST",
            "url": url,
            "json": json
        }
        r = self.send_api(r)
        # 获取token
        token = jsonpath.jsonpath(r.json(), "$.data.token")[0]
        logger.info(f"获取到的token值为{token}")
        return token

    def get_config(self):
        """
        读取yaml文件信息
        :return:
        """
        file_path = f"{Utils.get_file_path()}/config/test_dev.yaml"
        logger.info(f"文件路径为 {file_path}")
        yaml_data = Utils.get_yaml_data(file_path)
        self.base_url = yaml_data["base_url"]
        # 获取yaml文件中的用户名
        self.username = yaml_data["username"]
        # 获取yaml文件中的密码
        self.password = yaml_data["password"]
